from typing import TYPE_CHECKING, Dict

from common.equipment.reference import EQUIPMENT_SLOTS, Equipment
from common.player.bag import Bag
from common.player.quest_book import QuestBook
from common.player.state import PlayerState
from gui.reference import Pos
from utils.db import DBSession

if TYPE_CHECKING:
    from common.world import LatticeMap


class Player:
    def __init__(self, name):
        self.name = name
        self.map_name = 'player_home'
        self.pos = Pos(4, 4)

        self.state = PlayerState()
        self.quest_book = QuestBook()
        self.bag = Bag()
        self.equip_slots: Dict[str, Equipment | None] = {k: None for k in EQUIPMENT_SLOTS}

    def transfer_map(self, map_name):
        self.map_name = map_name
        setattr(self, '_lattice_map', DBSession.lattice_map[map_name])

    def get_local_map(self) -> 'LatticeMap':
        if not (lattice_map := getattr(self, '_lattice_map', None)):
            lattice_map = DBSession.lattice_map[self.map_name]
            setattr(self, '_lattice_map', lattice_map)

        return lattice_map

    def attack(self):
        attack = self.state.attack
        weapon: Equipment = self.equip_slots['weapon']
        if weapon is not None:
            attack += weapon.attrs['attack']

        return attack
